package com.game.server.db.dao;

import com.game.majiang.config.LogManager;
import com.game.server.db.Parameter;
import com.game.server.db.ParameterType;
import com.game.server.db.SqlHelper;
import com.game.server.db.entity.UserInfoData;
import com.game.server.db.entity.XyClubInfo;
import com.game.server.util.GetBeanFromResultUtil;

import javax.sql.rowset.CachedRowSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

public class UsersDao {

    public static HashMap<Integer, UserInfoData>  selectAllUserData() {//查询所有用户数据
        HashMap<Integer, UserInfoData> mapUsers = new HashMap<>();
        String sql = "SELECT id,nickname as name,head,sex,autograph,logintime FROM users";
        CachedRowSet resultSet = SqlHelper.getResultSet(sql);
        try {
            while (resultSet.next()) {
                Map<String, String> aliasMap = new HashMap<>();
                aliasMap.put("name","nickname");
                UserInfoData info = GetBeanFromResultUtil.getBean(resultSet, UserInfoData.class,aliasMap);
                LogManager.getInstance().Log("初始化用数据："+info.getId());
                LogManager.getInstance().Log(info.toString());
                mapUsers.put(info.getId(), info);
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return mapUsers;
    }

    public static void updateJiFen(int uid, Integer value) {
        String sql = "update users set integral=integral+? where id=?";
        Parameter p1 = new Parameter(Types.INTEGER, value, ParameterType.Input);
        Parameter p2 = new Parameter(Types.INTEGER, uid, ParameterType.Input);
        SqlHelper.execute(sql, p1, p2);
    }
    
    /**
     * 修改 uid 的 usebobo
     * @param uid
     * @param bobo
     */
    public static void updateUidToUseBobo(int uid, int bobo) {
    	String sql = "UPDATE users SET usebobo = usebobo + ? WHERE id = ?";
    	Parameter pid = new Parameter(Types.INTEGER, uid, ParameterType.Input);
    	Parameter p1 = new Parameter(Types.INTEGER, bobo, ParameterType.Input);
    	SqlHelper.execute(sql, p1, pid);
    }
    public static void updateUseBobo() {
    	String sql = "UPDATE users SET usebobo = 0";
    	SqlHelper.execute(sql);
    }
    
    /**
     * 启动游戏 把未加入到积分的 加上
     */
    public static void updateIntegral() {
    	String sql = "UPDATE users SET integral = integral + usebobo ";
    	SqlHelper.execute(sql);
    	updateUseBobo();
    }
    
}
